{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DatetimeIndex(['2226-01-01', '2228-01-01', '2230-01-01', '2232-01-01',\n",
      "               '2234-01-01', '2236-01-01', '2238-01-01', '2240-01-01',\n",
      "               '2242-01-01', '2244-01-01', '2246-01-01'],\n",
      "              dtype='datetime64[ns]', freq='2AS-JAN')\n",
      "DatetimeIndex(['2014-01-01', '2014-03-01', '2014-05-01', '2014-07-01',\n",
      "               '2014-09-01', '2014-11-01', '2015-01-01', '2015-03-01',\n",
      "               '2015-05-01', '2015-07-01', '2015-09-01'],\n",
      "              dtype='datetime64[ns]', freq='2MS')\n",
      "DatetimeIndex(['2014-01-01', '2014-01-03', '2014-01-05', '2014-01-07',\n",
      "               '2014-01-09', '2014-01-11', '2014-01-13', '2014-01-15',\n",
      "               '2014-01-17', '2014-01-19', '2014-01-21'],\n",
      "              dtype='datetime64[ns]', freq='2D')\n",
      "DatetimeIndex(['2014-01-01 00:00:00', '2014-01-01 02:00:00',\n",
      "               '2014-01-01 04:00:00', '2014-01-01 06:00:00',\n",
      "               '2014-01-01 08:00:00', '2014-01-01 10:00:00',\n",
      "               '2014-01-01 12:00:00', '2014-01-01 14:00:00',\n",
      "               '2014-01-01 16:00:00', '2014-01-01 18:00:00',\n",
      "               '2014-01-01 20:00:00'],\n",
      "              dtype='datetime64[ns]', freq='2H')\n",
      "DatetimeIndex(['2014-01-01 00:00:00', '2014-01-01 00:02:00',\n",
      "               '2014-01-01 00:04:00', '2014-01-01 00:06:00',\n",
      "               '2014-01-01 00:08:00', '2014-01-01 00:10:00',\n",
      "               '2014-01-01 00:12:00', '2014-01-01 00:14:00',\n",
      "               '2014-01-01 00:16:00', '2014-01-01 00:18:00',\n",
      "               '2014-01-01 00:20:00'],\n",
      "              dtype='datetime64[ns]', freq='2T')\n",
      "DatetimeIndex(['2014-01-01 00:00:00', '2014-01-01 00:00:02',\n",
      "               '2014-01-01 00:00:04', '2014-01-01 00:00:06',\n",
      "               '2014-01-01 00:00:08', '2014-01-01 00:00:10',\n",
      "               '2014-01-01 00:00:12', '2014-01-01 00:00:14',\n",
      "               '2014-01-01 00:00:16', '2014-01-01 00:00:18',\n",
      "               '2014-01-01 00:00:20'],\n",
      "              dtype='datetime64[ns]', freq='2S')\n"
     ]
    }
   ],
   "source": [
    "# date_range中时间单位的设定\n",
    "print pd.date_range('2014-01-01', periods=11, freq='2AS-JAN') # Y会以最后一日为时间\n",
    "print pd.date_range('2014-01-01', periods=11, freq='2MS') # M会以最后一日为时间\n",
    "print pd.date_range('2014-01-01', periods=11, freq='2D')\n",
    "print pd.date_range('2014-01-01', periods=11, freq='2h')\n",
    "print pd.date_range('2014-01-01', periods=11, freq='2min')\n",
    "print pd.date_range('2014-01-01', periods=11, freq='2S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1954-01-01 00:00:00\n",
      "1954-01-01\n",
      "1954-01-01\n",
      "<class 'pandas._libs.tslibs.timestamps.Timestamp'>\n"
     ]
    }
   ],
   "source": [
    "# 将datetime64转为一般的datetime\n",
    "times=pd.date_range('1954', periods=11, freq='1AS-JAN')\n",
    "time_format = \"%Y-%m-%d\"\n",
    "print(times[0].to_pydatetime())\n",
    "print(datetime.strftime(times[0].to_pydatetime(), time_format))\n",
    "print(times[0].strftime(time_format))\n",
    "print(type(times[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20190101\n",
      "20190101\n",
      "time.struct_time(tm_year=2019, tm_mon=1, tm_mday=1, tm_hour=10, tm_min=10, tm_sec=20, tm_wday=1, tm_yday=1, tm_isdst=-1)\n",
      "1546308620.0\n"
     ]
    }
   ],
   "source": [
    "# python的datetime\n",
    "from datetime import datetime\n",
    "import time\n",
    "\n",
    "dt = datetime(2019, 1, 1, 10, 10 ,20)\n",
    "# datetime转字符串\n",
    "print(dt.strftime('%Y%m%d'))\n",
    "print(datetime.strftime(dt, '%Y%m%d'))\n",
    "# datetime转utc时间戳\n",
    "import time\n",
    "print(dt.timetuple())\n",
    "print(time.mktime(dt.timetuple()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(Timestamp('1677-09-21 00:12:43.145225'), Timestamp('2262-04-11 23:47:16.854775807'))\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "argument must be 9-item sequence, not Timestamp",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-8-b5f5c064bc23>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mtime\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[1;32mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmktime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTimestamp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmin\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtime\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmktime\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTimestamp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmax\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: argument must be 9-item sequence, not Timestamp"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "time_sample = '2304-01-01'\n",
    "print(pd.Timestamp.min, pd.Timestamp.max)\n",
    "# pd.date_range(datetime.strptime(str(time_sample), '%Y-%m-%d'), periods=10 + 1,\n",
    "#                                   freq=\"2MS\", closed='right')\n",
    "\n",
    "import time\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2304, 1, 1, 0, 0)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.strptime(str(time_sample), '%Y-%m-%d')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
